package com.jsnkj.basic.activity

import androidx.lifecycle.MutableLiveData
import com.jsnkj.basic.bean.ErrorEntity
import com.jsnkj.basic.bean.UserDataEntity
import com.jsnkj.basic.http.HttpApiService
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.LogUtils
import com.jsnkj.basic.util.SharePreferencesUtil
import com.jsnkj.basic.util.jsonToString
import com.yang.http.HttpRequest
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import java.util.TreeMap

class LoginViewModel : HttpViewModel() {


    var postHttpResult: MutableLiveData<UserDataEntity> = MutableLiveData()

    var errorResult: MutableLiveData<ErrorEntity> = MutableLiveData()

    /**
     * 登录（测试专用）
     */
    fun postAuthLogin(openid: String) {
        launchHttp({
            val postMap = TreeMap<String, Any> { p0, p1 -> p0.compareTo(p1) }

            postMap["openid"] = openid

            HttpRequest.getService(HttpApiService::class.java).postAuthLogin(
                com.jsnkj.basic.util.AESUtils.encrypt(postMap.jsonToString()).jsonToString()
                    .toRequestBody("application/json;charset=UTF-8".toMediaTypeOrNull())
            )
        }, onResult = { it ->
            LogUtils.e("请求成功：$it")
            postHttpResult.value = it
        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
            LogUtils.e("NetworkViewModel", " postHomePageList commonLog - onFailed: $code, $msg")
        }) {
            LogUtils.e("请求成功", "postAppInit onComplete")
        }
    }


    /**
     * 登录
     */
    fun postOauthLogin(wxCode: String,driver:String) {
        launchHttp({
            val postMap = TreeMap<String, Any> { p0, p1 -> p0.compareTo(p1) }

            postMap["driver"] = driver
            postMap["code"] = wxCode

            postMap["pid"] =
                if (SharePreferencesUtil.getInstance().getString("pid").isNullOrEmpty())
                    ""
                else
                    SharePreferencesUtil.getInstance().getString("pid")!!

            postMap["cid"] =
                if (SharePreferencesUtil.getInstance().getString("cid").isNullOrEmpty())
                    ""
                else
                    SharePreferencesUtil.getInstance().getString("cid")!!

            postMap["group_invite"] =
                if (SharePreferencesUtil.getInstance().getString("group_invite").isNullOrEmpty())
                    "0"
                else
                    SharePreferencesUtil.getInstance().getString("group_invite")!!

            HttpRequest.getService(HttpApiService::class.java).postOauthLogin(
                com.jsnkj.basic.util.AESUtils.encrypt(postMap.jsonToString()).jsonToString()
                    .toRequestBody("application/json;charset=UTF-8".toMediaTypeOrNull())
            )
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("请求成功：$it")
            postHttpResult.value = it
        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
            com.jsnkj.basic.util.LogUtils.e("登录失败- onFailed: $code, $msg")
        }) {
            //LogUtils.e("请求成功", "postAppInit onComplete")
        }
    }






}